package com.order.controller;

import com.entity.Order;
import com.entity.Stock;
import com.fegin.StockService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.order.service.OrderService;
import com.utils.Result;
import com.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    private StockService stockService;
    @Autowired
    private OrderService orderService;

    @Autowired(required = false)
    DiscoveryClient discoveryClient;

    @GetMapping("/findOrder")
    public Result findOrder(){
        Result orderInfo = stockService.findStock();
        return orderInfo;
    }

    @GetMapping("/discoverClient")
    public Result discoverClient(){
        List<InstanceInfo> order = discoveryClient.getInstancesById("order");
       if (order.size()>0&&!order.isEmpty()) {
           for (InstanceInfo instanceInfo : order) {
           String instance = instanceInfo.getIPAddr()+"---"+instanceInfo.getPort()+"---"+instanceInfo.getInstanceId();
               return ResultUtils.ok(instance);
           }
       }
        return null;
    }

}
